<template>
  <div>
    <h1>品类列表</h1>
    <template>
      <div style="width: 300px">
        <div class="block">
          <el-tree
            :data="data"
            node-key="productsId"
            default-expand-all
            :expand-on-click-node="false"
            :props="defaultProps"
          >
            <span class="custom-tree-node" slot-scope="{ node, data }">
              <span>{{ node.label }}</span>

              <span>
                <el-button type="text" size="mini" @click="() => append(data)">
                  Append
                </el-button>

                <el-button
                  type="text"
                  size="mini"
                  @click="() => remove(node, data)"
                  v-show="data.parentsId != 0"
                >
                  Delete
                </el-button>
              </span>
            </span>
          </el-tree>
        </div>
      </div>
    </template>
  </div>
</template>

<script>
import Api from "@/api/afterUser/afterUserCRUD";

export default {
  inject: ["reload"],
  data() {
    const data = [{}];
    return {
      data: JSON.parse(JSON.stringify(data)),
      data: JSON.parse(JSON.stringify(data)),
      defaultProps: {
        children: "children",
        label: "productsName",
        id: "productsId",
      },
      form: {
        parentsId: undefined,
        productsId: undefined,
        productsName: "",
      },
    };
  },
  created() {
    this.fourKindList();
  },
  methods: {
    append(node, data) {
      this.form.parentsId = node.productsId;
      this.$prompt("请输入", "增加节点", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        inputPattern: /^[\u4e00-\u9fa5]{0,}$/, //匹配全中文
        inputErrorMessage: "请输入中文", //不符合正则匹配的的提示语句
      })
        .then(({ value }) => {
          //可以在这里发送http请求
          this.form.productsName = value;
          Api.fourAddKind(this.form)
            .then((ref) => {
              this.reload();
            })
            .catch((err) => {
              console.log("查询信息失败");
            });
        })
        .catch((err) => {});
    },

    remove(node, data) {
      this.form.productsId = data.productsId;
      Api.fourdelKind(this.form)
        .then((ref) => {
          console.log("删除节点成功");
          this.reload();
        })
        .catch((err) => {
          console.log("删除失败");
        });
    },
    // 查询品类
    fourKindList() {
      Api.fourKindList()
        .then((ref) => {
          // this.tableData = ref.data.data
          this.data = ref.data.data;
        })
        .catch((err) => {
          console.log("查询信息失败");
        });
    },
  },
};
</script>

<style>
.custom-tree-node {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: space-between;
  font-size: 14px;
  padding-right: 8px;
}
.custom-tree-container {
  margin-left: 10px;
  height: 100%;
  width: 300px;
  border-style: dashed;
  border-width: 1px;
}
</style>